# -*- coding: utf-8 -*-
"""
@time：  2021/06/19  16:13
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function: excel统一调用，支持xls，xlsx

"""
import os

from common.Logger import logger
from common.Excels import OldExcel
from common.Excels import NewExcel

def get_reader(srcfile=''):
    """
     获取读取excel的对象
    :param srcfile: excel文件路径
    :return:读取excel的对象
    """
    reader = None
    # 如果打开的文件不存在，就报错
    if not os.path.isfile(srcfile):
        logger.error("%s not exist!" % (srcfile))

    if srcfile.endswith('.xls'):
        reader = OldExcel.Reader()
        reader.open_excel(srcfile)
        return reader

    if srcfile.endswith('.xlsx'):
        reader = NewExcel.Reader()
        reader.open_excel(srcfile)
        return reader

def get_writer(srcfile='', dstfile=''):
    """
    获取写入excel的对象
    :param srcfile: excel远文件路径
    :param dstfile: 写入后excel文件保存的路径
    :return: 写入excel的对象
    """
    writer = None

    # 如果打开的文件不存在，就报错
    if not os.path.isfile(srcfile):
        logger.error("%s not exist!" % (srcfile))
        return writer

    if srcfile.endswith('xls'):
        writer = OldExcel.Writer()
        writer.copy_open(srcfile,dstfile)
        return writer

    if srcfile.endswith('xlsx'):
        writer = NewExcel.Writer()
        writer.copy_open(srcfile,dstfile)
        return writer

# 调试
if __name__ == '__main__':
    reader = get_reader('../lib/接口测试用例.xlsx')
    sheetname = reader.get_sheets()
    print(sheetname)
    for sheet in sheetname:
        # 设置当前读取的sheet页面
        reader.set_sheet(sheet)
        lines = reader.readline()
        for i in range(1,len(lines)):
            print(lines[i])

    # writer = get_writer('../lib/接口测试用例.xlsx','../lib/接口测试用例result.xlsx')
    # sheetname = writer.get_sheets()
    # writer.set_sheet(sheetname[0])
    # writer.write(1, 1, 'XINGYE',3)
    # writer.save_close()